/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bresenham;

import processing.core.PApplet;

/**
 *
 * @author DAVINCI
 */
public class Bresenham extends PApplet {

    int xStart;
    int yStart;
    int xEnd;
    int yEnd;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        PApplet.main(new String[]{"bresenham.Bresenham"});
    }

    @Override
    public void setup() {
        size(800, 600);
        background(200);

    }

    @Override
    public void draw() {
    }

    public void BresenhamLine(int xStart, int yStart, int xEnd, int yEnd) {
        int dx = xEnd - xStart;
        int dy = yEnd - yStart;
        int d = 2 * dy - dx;
        int x = xStart;
        int y = yStart;
        int incrE = 2 * dy;
        int incrNE = 2 * (dy - dx);

        if (abs(dx) > abs(dy)) {

            if (xStart < xEnd) {
                //octante 1
                if (yStart < yEnd) {
                    for (int i = 0; i <= abs(dx); i++) {
                        point(x, y);
                        if (d <= 0) {
                            d += incrE;
                            x++;
                        } else {
                            d += incrNE;
                            x++;
                            y++;
                        }
                    }
                    //octante 8
                }else{
                    d = 2 * dy + dx;
                    incrNE = 2 * (dy + dx);

                    for (int i = 0; i <= abs(dx); i++) {
                        point(x, y);
                        if (d >= 0) {
                            d += incrE;
                            x++;
                        } else {
                            d += incrNE;
                            x++;
                            y--;
                        }
                    }
                }
            } else{
                //octante 4
                if (yStart < yEnd) {
                    incrE = (-2) * dy;
                    incrNE=2 * (-dy - dx);
                    for (int i = 0; i <= abs(dx); i++) {
                        point(x, y);
                        if (d >= 0) {
                            d += incrE;
                            x--;
                        } else {
                            d += incrNE;
                            x--;
                            y++;
                        }
                    }
                    //octante 5
                }else{
                    d = 2 * (-dy) + dx;
                    incrE = (-2) * dy;
                    incrNE = 2 * (-dy + dx);
                    for (int i = 0; i <= abs(dx); i++) {
                        point(x, y);
                        if (d <= 0) {
                            d += incrE;
                            x--;
                        } else {
                            d += incrNE;
                            x--;
                            y--;
                        }
                    }
                }
            }
        }else{
            if (xStart < xEnd) {
                //octante 2
                   if (yStart < yEnd) {
                   
                    incrE = (-2) * dx;
                    incrNE = 2 * (dy - dx);
                    for (int i = 0; i <= abs(dy); i++) {
                        point(x, y);
                        if (d >= 0) {
                            d += incrE;
                            y++;
                        } else {
                            d += incrNE;
                            x++;
                            y++;
                        }
                    }
                    //octante 7
                }else{
                    d = dy +2* dx;
                    incrE = (2) * dx;
                    incrNE = 2 * (dy + dx);

                    for (int i = 0; i <= abs(dy); i++) {
                        point(x, y);
                        if (d <= 0) {
                            d += incrE;
                            y--;
                        } else {
                            d += incrNE;
                            x++;
                            y--;
                        }
                    }
                }
            } else{
                //octante 3
                if (yStart < yEnd) {
                    d=(-dy)-(2)*dx;
                    incrE = (-2) * dx;
                    incrNE=2 * (-dy - dx);
                    for (int i = 0; i <= abs(dy); i++) {
                        point(x, y);
                        if (d <= 0) {
                            d += incrE;
                            y++;
                        } else {
                            d += incrNE;
                            x--;
                            y++;
                        }
                    }
                    //octante 6
                }else{
                    d =  (-dy) - 2*dx;
                    incrE = (2) * dx;
                    incrNE = 2 * (-dy + dx);
                    for (int i = 0; i <= abs(dy); i++) {
                        point(x, y);
                        if (d >= 0) {
                            d += incrE;
                            y--;
                        } else {
                            d += incrNE;
                            x--;
                            y--;
                        }
                    }
                }
            }

        }




    }

    @Override
    public void mousePressed() {
        xStart = mouseX;
        yStart = mouseY;

    }

    @Override
    public void mouseDragged() {
        background(200);
        xEnd = mouseX;
        yEnd = mouseY;
        BresenhamLine(xStart, yStart, xEnd, yEnd);
//        BresenhamLine(5, 8,90,90);
    }
}
